package com.iuie.ns.frame.utils;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

public class WebUtils extends org.apache.shiro.web.util.WebUtils{
	
	/**
	 * 获取用户的IP地址
	 * @param request
	 * @return
	 * @date 2017年8月6日 下午7:49:17
	 */
	public static String getIP(HttpServletRequest request) {
		String ip = request.getHeader("X-Forwarded-For");
        if(StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)){
            //多次反向代理后会有多个ip值，第一个ip才是真实ip
            int index = ip.indexOf(",");
            if(index != -1){
                return ip.substring(0,index);
            }else{
                return ip;
            }
        }
        ip = request.getHeader("X-Real-IP");
        if(StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)){
            return ip;
        }
        return request.getRemoteAddr();
	}

	public static String getIP(ServletRequest request) {
		return WebUtils.getIP(WebUtils.toHttp(request));
	}

}
